package com.wokeMy.view.channe.huixiang;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.woke.MybankblindActivity;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.woke.data.Data_bindcarlist;
import com.woke.data.UnionPayChannel;
import com.woke.method.MyApp;
import com.woke.serizedatas.Datas_load;
import com.wokeMy.view.base.BaseActivity;
import com.wokeMy.view.html.RhkjzfHtmlActivity;
import com.wokeMy.view.model.Constant;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

import org.apache.http.Header;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;


public class HuixiangzfActivity extends BaseActivity implements View.OnClickListener{
    ImageView back_huixzf_iv;
    RelativeLayout zfcard_huixzf_rl,name_huixzf_rl;
    Button zhifu_huixzf_bt;
    TextView khk_huixzf_tv,name_huixzf_tv;

    Data_bindcarlist listdata;
    UnionPayChannel unionPayChannel;
    String pay_card_no,shou_card_no;
    String user_id;
    int way_id;
    String thirdPayment;
    int payWay;
    String useName,merchantName,merchantBillName,legalPersonID,merchantPersonPhone;
    float amount;
    String controller;

    String payee_bank_name,payee_branch_code;

    Dialog dialog;
    @Override
    public void setContentView() {
        setContentView(R.layout.activity_huixian_zf);
    }

    @Override
    public void initActivity() {
        back_huixzf_iv.setOnClickListener(this);
        zfcard_huixzf_rl.setOnClickListener(this);
        name_huixzf_rl.setOnClickListener(this);
        zhifu_huixzf_bt.setOnClickListener(this);
  
    }

    @Override
    public void initView() {
        back_huixzf_iv = myFindViewById(R.id.back_huixzf_iv);
        zfcard_huixzf_rl = myFindViewById(R.id.zfcard_huixzf_rl);
        khk_huixzf_tv = myFindViewById(R.id.khk_huixzf_tv);
        name_huixzf_rl = myFindViewById(R.id.name_huixzf_rl);
        name_huixzf_tv = myFindViewById(R.id.name_huixzf_tv);
        zhifu_huixzf_bt = myFindViewById(R.id.zhifu_huixzf_bt);

 
    }

    @Override
    public void initData() {
        MyApp myApp=(MyApp)getApplication();
        Intent intent=getIntent();
        Datas_load datas_load=myApp.getDatas_load();
        user_id=datas_load.getId();

        unionPayChannel = (UnionPayChannel)(intent.getSerializableExtra("UnionPayChannel"));
        amount=intent.getFloatExtra ("money", 0.01f);
        Log.e("unionPayChannel",unionPayChannel.toString());
        way_id=unionPayChannel.getWay();
        thirdPayment =unionPayChannel.getThirdPayment();
        payWay=unionPayChannel.getKind();
        controller=unionPayChannel.getController();
        String name="名字为空";
        if (!datas_load.getCom_name().equals("")){
            name=datas_load.getCom_name();
        }else {
            name=datas_load.getUser_nicename();
        }
        useName=name;
        merchantName=name+"的商户";
        merchantBillName=name+"的商户";
        legalPersonID=datas_load.getUser_no();
        merchantPersonPhone=datas_load.getUser_login();

//        openRhGood();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_huixzf_iv:
                finish();
                break;
            case R.id.zfcard_huixzf_rl:
                Intent intent = new Intent (HuixiangzfActivity.this, MybankblindActivity.class);
                intent.putExtra ("czhiortxian", "huixzf");
                startActivityForResult (intent, Constant.REQUEST_HXKJZF);
                break;
            case R.id.name_huixzf_rl:
                break;
            case R.id.zhifu_huixzf_bt:
                    if (pay_card_no==null||pay_card_no.equals("")){
                        Toast.makeText(this,"请选择支付卡",Toast.LENGTH_SHORT).show();
                    } else{
                        if (!fastClick()){
                            dialog= Util.createLoadingDialog(HuixiangzfActivity.this);
                            huixiangZhifu();
                        }
                    }

                break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        Log.e("onActivityResult","requestCode"+requestCode+"resultCode"+resultCode);
        if (resultCode==Constant.RESPONSE_HXKJZF){
            listdata=(Data_bindcarlist)data.getSerializableExtra("listdata");
            pay_card_no=listdata.cardno;
            khk_huixzf_tv.setText(pay_card_no);
//            isKaitongZhifu();
        }
    }

    //汇享快捷支付
    public void huixiangZhifu(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("card_no",pay_card_no);
        params.put("controller",controller);
        params.put("trans_amount",amount);
        params.put("id_card",legalPersonID);
        Log.e("汇享快捷支付params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.HXKJZF_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Util.closeDialog(dialog);
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("汇享快捷支付", result);
                    try {
                        JSONObject  jsonObject =new JSONObject(result);
//                        Intent intent =new Intent(RhKjzfActivity.this, RhkjzfHtmlActivity.class);
//                        intent.putExtra("rhkjzfhtml",rhkjzfhtml);
//                        startActivity(intent);


                        if (jsonObject.getString("flag").equals("success")){
                            JSONObject jsonObject1=jsonObject.getJSONObject("data").getJSONObject("data");
                            //看result 0未开通 1开通T0 2 开通T1 3开通 T1,T0 都开同
                            String result1=jsonObject1.getString("resp_code");
                            if (result1.equals("000000")){
                                String html=jsonObject1.getString("page_content");
                                Intent intent =new Intent(HuixiangzfActivity.this,RhkjzfHtmlActivity.class);
                                intent.putExtra("rhkjzfhtml",html);
                                startActivity(intent);
                            }else {
                                Toast.makeText(HuixiangzfActivity.this,jsonObject1.getString("respmsg"),Toast.LENGTH_SHORT).show();
                            }
                        }else {
                            Toast.makeText(HuixiangzfActivity.this,"11111111111",Toast.LENGTH_SHORT).show();
                            if (jsonObject.getJSONObject("data").getJSONObject("data").getString("resp_code").equals("999999")){
                                JSONObject jsonObject1=jsonObject.getJSONObject("data").getJSONObject("data");
                                payee_branch_code=jsonObject1.getString("payee_branch_code");
                                payee_bank_name=jsonObject1.getString("payee_bank_name");
                                shou_card_no=jsonObject1.getString("payee_bank_no");
                                aralogid("提示",jsonObject1.getString("resp_msg"));
                            }else {
                                Toast.makeText(HuixiangzfActivity.this,jsonObject.getString("info"),Toast.LENGTH_SHORT).show();
                            }
                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });
    }
    //汇享 更改 费率
    public void hxchangeFl(){
        AsyncHttpClient ahc = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        params.put("user_id",user_id);
        params.put("way",way_id);
        params.put("id_card",legalPersonID);//身份证号码
        params.put("payee_bank_no",shou_card_no);//收款卡（必须要储蓄卡）
        params.put("merchant_name",merchantName);
        params.put("merchant_province","福建省");
        params.put("merchant_city","厦门市");
        params.put("merchant_address","福建省厦门市思明区虎园路2号");
        params.put("mobile",merchantPersonPhone); // 收款卡预留手机号码(假的)
        params.put("payee_bank_name",payee_bank_name);//失败反回来的
        params.put("payee_branch_name","工商银行厦门软件园支行");
        params.put("payee_branch_code",payee_branch_code);
        params.put("payee_bank_province","福建省");
        params.put("payee_bank_city","厦门市");
        Log.e("汇享更改费率params",params.toString());
        //url:   parmas：请求时携带的参数信息   responseHandler：是一个匿名内部类接受成功过失败
        ahc.post(Constant.HXCXFL_URL, params, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Util.closeDialog(dialog);
                String result = null;
                try {
                    result = new String(bytes, "utf-8");
                    //                        Toast.makeText(SeletCardActivity.this, result, Toast.LENGTH_SHORT).show();
                    Log.e("汇享更改费率", result);
                    try {
                        JSONObject  jsonObject =new JSONObject(result);
//                        Intent intent =new Intent(RhKjzfActivity.this, RhkjzfHtmlActivity.class);
//                        intent.putExtra("rhkjzfhtml",rhkjzfhtml);
//                        startActivity(intent);


//                        if (jsonObject.getString("flag").equals("success")){
//                            JSONObject jsonObject1=jsonObject.getJSONObject("data").getJSONObject("data");
//                            //看result 0未开通 1开通T0 2 开通T1 3开通 T1,T0 都开同
//                            String result1=jsonObject1.getString("resp_code");
//                            if (result1.equals("000000")){
//                                String html=jsonObject1.getString("page_content");
//                                Intent intent =new Intent(HuixiangzfActivity.this,RhkjzfHtmlActivity.class);
//                                intent.putExtra("rhkjzfhtml",html);
//                                startActivity(intent);
//                            }else {
//                                Toast.makeText(HuixiangzfActivity.this,jsonObject1.getString("respmsg"),Toast.LENGTH_SHORT).show();
//                            }
//                        }else {
//                            Toast.makeText(HuixiangzfActivity.this,jsonObject.getString("info"),Toast.LENGTH_SHORT).show();
//
//                        }

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Util.closeDialog(dialog);
            }
        });
    }
    AlertDialog alertDialog;
    private void aralogid(String info, String kll) {
        alertDialog = new AlertDialog.Builder(HuixiangzfActivity.this).
                setTitle(info).
                setMessage(kll).
                setPositiveButton("重新", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        hxchangeFl();

                    }
                }).
                setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        alertDialog.dismiss();
                    }
                }).create();
        alertDialog.show();
    }
}
